package cn.com.w.dao;


import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.model.ClassAnnotationAttributes;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Lombok;
import lombok.NoArgsConstructor;

import java.sql.Types;

/**
 * @author w
 * @Description:
 * @DateTime: 2025 04 25 13:53
 **/
public class MybatisPlusGenerator {

    private static final String url = "jdbc:mysql://127.0.0.1:3306/login?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
    private static final String username = "root";
//    private static final String password = "123456";
    private static final String password = "root";
    public static void main(String[] args) {

        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> {
                    builder.author("w") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .outputDir("login_j\\dao\\src\\main\\java"); // 指定输出目录
                })
                .dataSourceConfig(builder ->
                        builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                            // 自定义类型转换
                            int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                            if (typeCode == Types.SMALLINT) {
                                return DbColumnType.INTEGER;
                            }
                            if (typeCode == Types.BIGINT) {
                                return DbColumnType.LONG;
                            }
                            return typeRegistry.getColumnType(metaInfo);
                        })
                )
                .packageConfig(builder ->
                        builder.parent("cn.com.w.dao")
                                .entity("Entity")
                                .service("Service")
                                .mapper("Mapper")
                                .xml("Mapper.xml")
                                .serviceImpl("Service.Impl")
                )
                .strategyConfig(builder ->
                        // 设置需要生成的表名
                        builder.addInclude("t_blog_tags")
                                .entityBuilder()
                                .enableLombok()
                                .addClassAnnotation(new ClassAnnotationAttributes(AllArgsConstructor.class, "@AllArgsConstructor"))
                                .addClassAnnotation(new ClassAnnotationAttributes(NoArgsConstructor.class,"@NoArgsConstructor"))
                                .addClassAnnotation(new ClassAnnotationAttributes(Builder.class,"@Builder"))
                                .enableTableFieldAnnotation()
                                .enableColumnConstant()
                                .controllerBuilder().disable()
                )
                // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}